//
//  TMGChangePayPwdViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/23.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGChangePayPwdViewController.h"
#import "STPasswordTextField.h"
#import "STSendButton.h"

@interface TMGChangePayPwdViewController ()<STSendButtonDlegate>
@property(nonatomic, strong) STPasswordTextField                     *pwdTextFiled;
@property(nonatomic, strong) STSendButton                     *sendButton;
@property(nonatomic, strong) UITextField                     *codeTextFiled;
@end

@implementation TMGChangePayPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改交易密码";
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    
     NSString * mobile = [STUserManger defult].loginUser.mobile;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 30)
                                                     text:[NSString stringWithFormat:@"当前手机号码为%@，将发送验证码到该手机号",mobile]
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [NSString lableAutoAdjustheightWithLabel:titleLable];
    [footer addSubview:titleLable];
    
    UIView * codeView = [self configCodeTextFiled];
    codeView.top = titleLable.bottom;
    [footer addSubview:codeView];

    
    self.pwdTextFiled = [[STPasswordTextField alloc] initWithFrame:CGRectMake(15, codeView.bottom + 10, UIScreenWidth - 30, 55) passwordLenth:6];
    self.pwdTextFiled.secureTextEntry = YES;
    [footer addSubview:self.pwdTextFiled];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"确认修改"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedCommitButton];
    }];
    confimButton.top = self.pwdTextFiled.bottom +  120;
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    footer.height = confimButton.bottom;
    self.tableView.tableFooterView = footer;
}
- (UIView*)configCodeTextFiled{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 44)];
    view.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0  blue:240/255.0  alpha:1];
    [view st_showBottomLine];
    UITextField * textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, view.st_width - 0, 43)];
    textFiled.placeholder = @"请输入验证码";
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 15)];
    UIImageView * leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
    leftImageView.image = [UIImage imageNamed:@"icon_验证码"];
    [leftView addSubview:leftImageView];
    textFiled.leftView = leftView;
    textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    //  textFiled.textColor = UIColorFromRGBA(0xB3B3B3);
    //倒计时
    self.sendButton = [[STSendButton alloc] initWithFrame:CGRectMake(0, 0, 100, 30) andWithDuration:60];
    self.sendButton.backgroundColor = FlatRed;
    self.sendButton.layer.cornerRadius = 15;
    self.sendButton.clipsToBounds = YES;
    self.sendButton.delegate = self;
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:12];
    textFiled.rightView = self.sendButton;
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    textFiled.rightViewMode = UITextFieldViewModeAlways;
    [view addSubview:textFiled];
    self.codeTextFiled = textFiled;
    // textFiled.textColor = UIColorFromRGBA(0xB3B3B3);
    return view;
}
- (BOOL)STSendButtonWillClic:(UIButton *)button{

    return YES;
}
- (void)STSendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    if (isFirstClic) {
        [self sendcodeRequest];
    }
}

- (void)STSendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:[NSString stringWithFormat:@"%ld秒",duration] forState:UIControlStateNormal];
}
- (void)STSendButtonTimeEnded:(UIButton*)button{
    [self.sendButton timerEnd];
    [self.sendButton setTitle:@"获取验证码" forState:UIControlStateNormal];
}
#pragma mark --NetWork Method
- (void)sendcodeRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * mobile = [STUserManger defult].loginUser.mobile;
    if (mobile.length) {
        [dic setObject:mobile forKey:@"mobile"];
    }
    [SVProgressHUD showWithStatus:@"正在发送"];
    [[STNetWrokManger defaultClient] requestWithPath:[NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/index/sendSms"]
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"验证码已成功发送"];
                                                 [self.sendButton timerBegin];
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [self.sendButton timerEnd];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

- (void)onSlectedCommitButton{
    if (self.codeTextFiled.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    if (self.pwdTextFiled.text.length < 6) {
        [SVProgressHUD showErrorWithStatus:@"请输入六位密码"];
        return;
    }
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/editPayPassword"];
    [dic setObject:self.pwdTextFiled.text forKey:@"pay_password"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    [dic setObject:self.codeTextFiled.text forKey:@"code"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"更改交易密码成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
